import { createVNode, render, type Component, type VNode } from 'vue'

export function createDivInBody(): HTMLDivElement {
  const div = document.createElement('div')
  document.body.appendChild(div)
  return div
}

export function renderInBody(cmp: Component, props?: Record<string, unknown>): VNode {
  const div = createDivInBody()
  const vNode = createVNode(cmp, props)
  render(vNode, div)
  return vNode
}

export function exposedOf<T, R = any>(vNode: VNode, op: (exposed: T) => R): R | null {
  const exposed = vNode.component?.exposed
  if (!exposed) {
    return null
  }
  return op(exposed as T)
}
